<?php

class sbHotels {
    public function hello() {
        return 'hello';
    }
    
    public function searchHotelsAjax($parameters) {
        $values = $this->searchHotelsToArray($parameters);
        
        if ($values['hotel'] == 0 || is_null($values['hotel'])) {
            $tempGoc = $this->searchGocByCity($values);
            $tempIsr = $this->searchIsrByCity($values);
        }
        
        $search = array();
        $local_name = array();
        $local_rate = array();
        
        array_push($search, $tempIsr['search']);
        array_push($local_name, $tempIsr['local_name']);
        array_push($local_rate, $tempIsr['local_rate']);
        
        array_push($search, $tempGoc['search']);
        array_push($local_name, $tempGoc['local_name']);
        array_push($local_rate, $tempGoc['local_rate']);
        
        return array('search' => $search[0], 'local_name' => $local_name[0], 'local_rate' => $local_rate[0], 'location' => $this->searchLocationToArray($values['city']));
    }
    
    public function searchIsrByCity($values) {
        $city = tblCityTable::getInstance()->findByDql('id=?', $values['city'])->getFirst();
        $region = tblRegionTable::getInstance()->findByDql('id=?', $city->getRegionId())->getFirst();
        
        $isr = new sbIsratel();
        $client = $isr->connect($values['civic']);
        $hotels = $isr->searchByRegion($client, $region->getCodeIsr());
        $parse = $isr->parseSearchByRegion($hotels, $city->getId(), $values['stars']);
        
        return $isr->insertLocalData($parse, $values['city']);
    }
    
    public function searchLocationToArray($city) {
        $objCity = tblCityTable::getInstance()->findByDql('id=?', $city)->getFirst();
        $objRegion = tblRegionTable::getInstance()->findByDql('id=?', $objCity->getRegionId())->getFirst();
        $objCountry = tblCountryTable::getInstance()->findByDql('id=?', $objCity->getCountryId())->getFirst();
        
        return array('Country' => $objCountry->toArray(), 'Region' => $objRegion->toArray(), 'City' => $objCity->toArray());
    }
    
    public function searchGocByCity($values) {
        $city = tblCityTable::getInstance()->findByDql('id=?', $values['city'])->getFirst();
        
        $goc = new sbGoc();
        $client = $goc->connect();
        $request = $goc->GetAvalibleRoomByCity($client['cause'], $city->getGocCode(), $values['date_in'], $values['days'], $values['rooms']);
        $parse = $goc->parseAvalRoom($request, $values['civic']);
        
        return $goc->insertLocalData($parse, $values['city']);
    }
    
    public function searchHotelsToArray($parameters) {
        return array(
            'city' => $parameters['city'],
            'hotel' => $parameters['hotel'],
            'date_in' => $parameters['date_in'],
            'date_out' => $parameters['date_out'],
            'days' => $parameters['days'],
            'stars' => $parameters['stars'],
            'rooms' => $parameters['rooms'],
            'civic' => $parameters['civic']
        );
    }

    public static function serachHotelsPanel($city_id, $hotel_id, $provider_id, $stars, $page) {
        $limStart = ($page * sbValues::HOTELS_IN_ADMIN_PAGE) - (sbValues::HOTELS_IN_ADMIN_PAGE);
        if ($city_id == 0 && $hotel_id == 0 && $provider_id == 0) {
            $hotels = Doctrine_Query::create()->
                            select('h.*, c.name_en, s.name_en')->
                            from('tblHotel h')->
                            leftJoin('h.CountryHotel c')->
                            leftJoin('h.CityHotel s')->
                            where('h.stars BETWEEN ? AND ?', array($stars[0], $stars[1]))->
                            offset($limStart)->
                            limit(sbValues::HOTELS_IN_ADMIN_PAGE)->execute();

            $rows = Doctrine_Query::create()->
                    select('COUNT(*)')->
                    from('tblHotel')->
                    where('stars BETWEEN ? AND ?', array($stars[0], $stars[1]))->
                    execute();
            $row = $rows[0]['COUNT'];
        } else if ($hotel_id > 0) {
            $hotels = Doctrine_Query::create()->
                            select('h.*, c.name_en, s.name_en')->
                            from('tblHotel h')->
                            leftJoin('h.CountryHotel c')->
                            leftJoin('h.CityHotel s')->
                            where('h.id=?', $hotel_id)->
                            offset($limStart)->
                            limit(sbValues::HOTELS_IN_ADMIN_PAGE)->execute();

            $row = 1;
        } else if ($city_id > 0 && $provider_id > 0) {
            $hotels = Doctrine_Query::create()->
                            select('h.*, c.name_en, s.name_en')->
                            from('tblHotel h')->
                            leftJoin('h.CountryHotel c')->
                            leftJoin('h.CityHotel s')->
                            where('h.city_id=? AND h.provider_id=? AND h.stars BETWEEN ? AND ?', array($city_id, $provider_id, $stars[0], $stars[1]))->
                            offset($limStart)->
                            limit(sbValues::HOTELS_IN_ADMIN_PAGE)->execute();

            $rows = Doctrine_Query::create()->
                    select('COUNT(*)')->
                    from('tblHotel')->
                    where('city_id=? AND provider_id=? AND stars BETWEEN ? AND ?', array($city_id, $provider_id, $stars[0], $stars[1]))->
                    execute();
            $row = $rows[0]['COUNT'];
        } else if ($city_id == 0 && $provider_id > 0) {
            $hotels = Doctrine_Query::create()->
                            select('h.*, c.name_en, s.name_en')->
                            from('tblHotel h')->
                            leftJoin('h.CountryHotel c')->
                            leftJoin('h.CityHotel s')->
                            where('h.provider_id=? AND h.stars BETWEEN ? AND ?', array($provider_id, $stars[0], $stars[1]))->
                            offset($limStart)->
                            limit(sbValues::HOTELS_IN_ADMIN_PAGE)->execute();

            $rows = Doctrine_Query::create()->
                    select('COUNT(*)')->
                    from('tblHotel')->
                    where('provider_id=? AND stars BETWEEN ? AND ?', array($provider_id, $stars[0], $stars[1]))->
                    execute();
            $row = $rows[0]['COUNT'];
        } else if ($city_id > 0 && $provider_id == 0) {
            $hotels = Doctrine_Query::create()->
                            select('h.*, c.name_en, s.name_en')->
                            from('tblHotel h')->
                            leftJoin('h.CountryHotel c')->
                            leftJoin('h.CityHotel s')->
                            where('h.city_id=? AND h.stars BETWEEN ? AND ?', array($city_id, $stars[0], $stars[1]))->
                            offset($limStart)->
                            limit(sbValues::HOTELS_IN_ADMIN_PAGE)->execute();

            $rows = Doctrine_Query::create()->
                    select('COUNT(*)')->
                    from('tblHotel')->
                    where('city_id=? AND stars BETWEEN ? AND ?', array($city_id, $stars[0], $stars[1]))->
                    execute();
            $row = $rows[0]['COUNT'];
        }

        $arrHotels = array();

        foreach ($hotels as $h) {
            $provider_name;
            switch ($h->getProviderId()) {
                case sbValues::PROVIDER_GOC:
                    $provider_name = 'GOC';
                    break;
                case sbValues::PROVIDER_ISR:
                    $provider_name = 'ISR';
                    break;
                case sbValues::PROVIDER_DAN:
                    $provider_name = 'DAN';
                    break;
            }

            array_push(
                    $arrHotels, array(
                'id' => $h->getId(),
                'name' => $h->getNameEn(),
                'stars' => $h->getStars(),
                'country_id' => $h->getCountryId(),
                'country_name' => $h['CountryHotel']['name_en'],
                'city_id' => $h->getCityId(),
                'city_name' => $h['CityHotel']['name_en'],
                'provider_id' => $h->getProviderId(),
                'provider_name' => $provider_name
                    )
            );
        }

        return array('hotels' => $arrHotels, 'row' => $row);
    }

    public static function searchHotels($city_id, $hotel_id, $date, $night, $stars, $rooms, $civic, $sort, $page) {
//        $troli = sfContext::getInstance()->getRequest()->getCookie('troli');
//get all hotels - in city, or hotel id
        if ($hotel_id > 0) {
            $hotels = Doctrine_Query::create()->
                    select('r.*, c.name_en, g.name_en, s.name_en')->
                    from('tblHotel r')->
                    leftJoin('r.CountryHotel c')->
                    leftJoin('r.RegionHotel g')->
                    leftJoin('r.CityHotel s')->
                    where('r.id=?', $hotel_id)->
                    execute();

            $rows = 1;
        } else {
            $limStart = ($page * sbValues::HOTELS_IN_PAGE) - (sbValues::HOTELS_IN_PAGE);
            if ($sort == sbValues::SORT_BY_NAME) {
                $hotels = Doctrine_Query::create()->
                        select('r.*, c.name_en, g.name_en, s.name_en')->
                        from('tblHotel r')->
                        leftJoin('r.CountryHotel c')->
                        leftJoin('r.RegionHotel g')->
                        leftJoin('r.CityHotel s')->
                        where('r.city_id=? AND r.stars BETWEEN ? AND ?', array($city_id, $stars[0], $stars[1]))->
                        orderBy('r.name_en ASC')->
                        offset($limStart)->
                        limit(sbValues::HOTELS_IN_PAGE)->
                        execute();
            } else if ($sort == sbValues::SORT_BY_RANK) {
                $hotels = Doctrine_Query::create()->
                        select('r.*, c.name_en, g.name_en, s.name_en')->
                        from('tblHotel r')->
                        leftJoin('r.CountryHotel c')->
                        leftJoin('r.RegionHotel g')->
                        leftJoin('r.CityHotel s')->
                        where('r.city_id=? AND r.stars BETWEEN ? AND ?', array($city_id, $stars[0], $stars[1]))->
                        orderBy('r.stars DESC')->
                        offset($limStart)->
                        limit(sbValues::HOTELS_IN_PAGE)->
                        execute();
            } else if ($sort == sbValues::SORT_BY_PRICE) {
                $hotels = Doctrine_Query::create()->
                        select('r.*, c.name_en, g.name_en, s.name_en')->
                        from('tblHotel r')->
                        leftJoin('r.CountryHotel c')->
                        leftJoin('r.RegionHotel g')->
                        leftJoin('r.CityHotel s')->
                        where('r.city_id=? AND r.stars BETWEEN ? AND ?', array($city_id, $stars[0], $stars[1]))->
                        orderBy('r.stars DESC')->
                        offset($limStart)->
                        limit(sbValues::HOTELS_IN_PAGE)->
                        execute();
            }

            $rows = Doctrine_Query::create()->
                    select('COUNT(*)')->
                    from('tblHotel')->
                    where('city_id=? AND stars BETWEEN ? AND ?', array($city_id, $stars[0], $stars[1]))->
                    execute();

            $rows = $rows[0]['COUNT'];
        }

        $arrHotels = array();
        foreach ($hotels as $h) {
            switch ($h->getProviderId()) {
                case sbValues::PROVIDER_GOC:
                    $temp = self::searchGocHotels($h, $date, $night, $rooms, $civic);
                    array_push($arrHotels, $temp);
                    break;
                case sbValues::PROVIDER_ISR:
                    $temp = self::searchIsratelHotels($h, $date, $night, $rooms, $civic);
                    array_push($arrHotels, $temp);
                    break;
                case sbValues::PROVIDER_DAN:
                    array_push($arrHotels, $h->toArray());
                    break;
            }
        }

        return array('hotels' => $arrHotels, 'rows' => $rows);
    }

    public static function searchHotel($hotel_id, $date, $night, $type) {
        $hotel = Doctrine_Query::create()->
                select('r.*, c.name_en, g.name_en, s.name_en')->
                from('tblHotel r')->
                leftJoin('r.CountryHotel c')->
                leftJoin('r.RegionHotel g')->
                leftJoin('r.CityHotel s')->
                where('r.id=?', $hotel_id)->
                execute();

        $rooms[0]['adult'] = 2;
        $rooms[0]['children'] = 0;
        $rooms[0]['baby'] = 0;

        switch ($hotel[0]->getProviderId()) {
            case sbValues::PROVIDER_GOC:
                $h = self::searchGocHotels($hotel[0], $date, $night, $rooms, $type);
                break;
            case sbValues::PROVIDER_ISR:
                $h = self::searchIsratelHotels($hotel[0], $date, $night, $rooms, $type);
                break;
            case sbValues::PROVIDER_DAN:
                $h = $hotel[0]->toArray();
                break;
        }

        return $h;
    }

    public static function searchIsratelHotels($hotel, $date, $days, $rooms, $type) {
        $isratel = new sbIsratel($type);
        $client = $isratel->connect();
        $result = $isratel->search($client, $hotel->getProviderCode(), $date, $days, $rooms);
        $price = $isratel->parseSearchResult($result);

        $rooms = new newHotelSearch();
        $arrRooms = $rooms->insertHotelRooms($hotel->getId(), $price);
        $hotel = $hotel->toArray();
        $hotel['Rooms'] = $arrRooms;

        return $hotel;
    }

    public static function searchGocHotels($hotel, $date, $days, $rooms, $civic) {
        if ($civic == sbValues::USER_TOURIST) {
            $url = sbValues::URL_USD_ISRATEL;
        } else {
            $url = sbValues::URL_USD_ISRATEL;
        }

        $goc = new sbGoc();
        $client = $goc->connect();
        $result = $goc->search($client['cause'], $hotel->getProviderCode(), $date, $days, $rooms);
        $price = $goc->parseSearchResult($result);

        $rooms = new newHotelSearch();
        $arrRooms = $rooms->insertHotelRooms($hotel->getId(), $price);
        $hotel = $hotel->toArray();
        $hotel['Rooms'] = $arrRooms;

        return $hotel;
    }
    
    public static function hotelData($hotel_id) {
        return tblHotelTable::getInstance()->findByDql('id=?', $hotel_id)->getFirst();
    }
    
    public function roomsData($hotel_id) {
        return tblRoomsTable::getInstance()->findByDql('hotel_id=?', $hotel_id);
    }
    
    public function countryInfo($country_id) {
        return tblCountryTable::getInstance()->findByDql('id=?', $country_id)->getFirst();
    }
    
    public function regionInfo($region_id) {
        return tblRegionTable::getInstance()->findByDql('id=?', $region_id)->getFirst();
    }
    
    public function cityInfo($city_id) {
        return tblCityTable::getInstance()->findByDql('id=?', $city_id)->getFirst();
    }


    public function moreHotel($hotel_id) {
        $objHotel = self::hotelData($hotel_id);
        $objCountry = $this->countryInfo($objHotel->getCountryId());
        $objRegion = $this->regionInfo($objHotel->getRegionId());
        $objCity = $this->cityInfo($objHotel->getCityId());
        $arrHotel = $objHotel->toArray();
        $arrCountry = $objCountry->toArray();
        $arrRegion = $objRegion->toArray();
        $arrCity = $objCity->toArray();
        
        $objRooms = $this->roomsData($hotel_id);
        $arrRooms = $objRooms->toArray();
        
        $arrHotel['Rooms'] = $arrRooms;
        $arrHotel['Country'] = $arrCountry;
        $arrHotel['Region'] = $arrRegion;
        $arrHotel['City'] = $arrCity;
        
        return $arrHotel;
    }

}
